﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace CustomException
{
    /// <summary>
    /// Classe de utilidades para envio de e-mails
    /// </summary>
    internal class EmailUtility
    {
        #region E-mail

        internal static void SendEmail(string smtpServer, int port, string from, string to, string about, string body, MailPriority priority, string replyTo = null, string authenticatedUser = null, string authenticatedPassword = null, bool useBodyHtml = true, bool enableSsl = false, bool useDefaultCredentials = true, string copyTo = null, string hiddenCopyTo = null, string[] attachments = null)
        {
            // O padrão de 'IsBodyHtml' é true
            using (var message = new MailMessage { From = new MailAddress(from.Trim().ToLower()), Subject = about, IsBodyHtml = useBodyHtml, Body = body })
            {
                // Anexos
                if (attachments != null && attachments.Length > 0)
                {
                    foreach (var attachment in attachments)
                        message.Attachments.Add(new Attachment(attachment));
                }

                if (!string.IsNullOrWhiteSpace(replyTo))
                    message.ReplyTo = new MailAddress(replyTo.Trim().ToLower());

                // Define a prioridade do envio do E-mail
                message.Priority = priority;

                using (var smtp = new SmtpClient(smtpServer.Trim().ToLower())
                {
                    // Porta SMTP
                    Port = port,
                    // O Padrão é false
                    EnableSsl = enableSsl,
                    // O Padrão é true
                    UseDefaultCredentials = useDefaultCredentials
                })
                {
                    // Para
                    var mailsTo = to.Trim().ToLower().Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var mailTo in mailsTo)
                        message.To.Add(mailTo);

                    // Com cópia
                    if (!string.IsNullOrWhiteSpace(copyTo))
                    {
                        var comCopias = copyTo.Trim().ToLower().Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var copia in comCopias)
                            message.CC.Add(copia);
                    }

                    // Com cópia oculta
                    if (!string.IsNullOrWhiteSpace(hiddenCopyTo))
                    {
                        var comCopiasOcultas = hiddenCopyTo.Trim().ToLower().Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var copiaOculta in comCopiasOcultas)
                            message.Bcc.Add(copiaOculta);
                    }

                    // O Padrão é nulo
                    if (!string.IsNullOrWhiteSpace(authenticatedUser) && !string.IsNullOrWhiteSpace(authenticatedPassword))
                        smtp.Credentials = new NetworkCredential(authenticatedUser.Trim().ToLower(), authenticatedPassword.Trim());

                    smtp.Send(message);
                }
            }
        }

        #endregion
    }
}
